package com.example.teachingevaluationsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teachingevaluationsystem.entity.Course;
import com.example.teachingevaluationsystem.entity.CourseOrder;
import com.example.teachingevaluationsystem.entity.User;
import com.example.teachingevaluationsystem.service.CourseOrderService;
import com.example.teachingevaluationsystem.service.CourseService;
import com.example.teachingevaluationsystem.service.IUserService;
import com.example.teachingevaluationsystem.vo.CourseVo;
import lombok.experimental.Delegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;

/**
 * 课程 前端控制器
 *
 * @author lp
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/evaluate/course")
public class CourseController {

  @Autowired private CourseService courseService;

  @Autowired private IUserService userService;

  @Autowired private CourseOrderService courseOrderService;

  /** 分页查询 课程信息 */
  @GetMapping("page")
  public Page<CourseVo> loadPage(Page<Course> coursePage, Course course) {
    return courseService.pageVo(coursePage, course);
  }

  /**
   * 保存或者更新课程信息
   *
   * @param course 课程信息
   * @return 是否成功
   */
  @PostMapping
  public boolean save(@RequestBody Course course) {
    return courseService.saveOrUpdate(course);
  }

  /**
   * 按照id删除课程信息
   *
   * @param ids 课程id 集合
   * @return 是否成功
   */
  @DeleteMapping
  public boolean deleteByIds(@RequestBody Long[] ids) {
    return courseService.removeBatchByIds(Arrays.asList(ids));
  }

  /** 获取课程通过课程id */
  @GetMapping("{courseId}")
  public Course info(@PathVariable("courseId") Long courseId) {
    return courseService.getById(courseId);
  }

  /** 通过状态计划Id查询当前课程评价情况 */
  @GetMapping("student")
  public Page<CourseVo> getCourseList(
      Page<CourseVo> courseVoPage, Principal principal, Long planId) {
    final String name = principal.getName();
    return courseService.loadCourseListByUserName(courseVoPage, name, planId);
  }

  @GetMapping("teacher")
  public Page<CourseVo> getTeacherCourseList(
      Page<CourseVo> courseVoPage, Principal principal, Long planId) {
    final String name = principal.getName();
    return courseService.loadCourseListByTeacherName(courseVoPage, name, planId);
  }

  @GetMapping("status/page")
  public Page<CourseVo> loadStudentEvaluationStatus(
      Page<CourseVo> courseVoPage, Principal principal, @RequestParam(defaultValue = "false")  Boolean isTeacher) {
    return courseService.loadEvaluationStatus(courseVoPage, principal.getName(), isTeacher);
  }

  @GetMapping("teacher/list")
  public List<Course> getCourseList(Principal principal) {
    final User user = userService.loadUserByUserName(principal.getName());
    return courseService.list(
        new LambdaQueryWrapper<Course>().eq(Course::getTeacherId, user.getId()));
  }

  @GetMapping("page/order")
  public Page<CourseVo> loadOrderPage(Principal principal, Page<Course> coursePage, Course course) {
    return courseService.pageOrderVo(principal.getName(), coursePage, course, true);
  }

  @GetMapping("page/noOrder")
  public Page<CourseVo> loadNoOrderPage(
      Principal principal, Page<Course> coursePage, Course course) {
    return courseService.pageOrderVo(principal.getName(), coursePage, course, false);
  }

  @PostMapping("order")
  public boolean order(Principal principal, Long courseId) {
    final User user = userService.loadUserByUserName(principal.getName());
    final CourseOrder entity = new CourseOrder();
    entity.setStudentId(user.getId());
    entity.setCourseId(courseId);
    return courseOrderService.save(entity);
  }

  @DeleteMapping("order")
  public boolean noOrder(Principal principal, Long courseId) {
    final User user = userService.loadUserByUserName(principal.getName());
    return courseOrderService.remove(
        new LambdaQueryWrapper<CourseOrder>()
            .eq(CourseOrder::getCourseId, courseId)
            .eq(CourseOrder::getStudentId, user.getId()));
  }

  @DeleteMapping("status")
  public void  deleteStatus(Principal principal,Long planId,Long courseId){
    final User user = userService.loadUserByUserName(principal.getName());
    courseOrderService.deleteStatus(user.getId(),planId,courseId);
  }
}
